# 自媒体发布文章页面对象
from selenium.webdriver.common.by import By

from base.mp.base import BasePage, BaseHandle
from until import UtilsDriver, choice_channel


# 定义对象库层
class PublishPage(BasePage):
    def __init__(self):
        super().__init__()
        # 文章名称输入框
        self.title = By.ID, "publish_page"
        # ifream元素对象
        self.iframe_ele = By.ID, "publish_page"
        # 文章内容输入框
        self.content = By.CSS_SELECTOR, "#publish_page img"
        # 封面选中
        self.cover = By.XPATH, "//*[@id='cover']"
        # 频道选则
        self.chanel = By.XPATH, "//*[@id='chanel']"
        # 发表按钮
        self.publish_btn = By.XPATH, "//*[@id='PublishButton']"

    # 定位文章名输入框
    def find_title(self):
        return self.get_element(self.title)

    # 定位iframe
    def find_iframe(self):
        return self.get_element(self.iframe_ele)

    # 定位文章内容输入框
    def find_content(self):
        return self.get_element(self.content)

    # 定位封面选中
    def find_cover(self):
        return self.get_element(self.cover)

    # 定位频道选则框
    def find_chanel(self):
        return self.get_element(self.chanel)

    # 定位发表按钮
    def find_publish_btn(self):
        return self.get_element(self.publish_btn)


# 定义操作层
class PublishHandle(BaseHandle):
    def __init__(self):
        self.publish_page = PublishPage()  # 实例化对象库层

    # 输入文章标题
    def input_title(self, title):
        self.input_text(self.publish_page.find_title(), title)

    # 输入文章内容
    def input_iframe(self, content):
        """
        -----切换到iframe中-----
        UtilsDriver.get_mp_driver():获取driver对象
        .switch_to.frame（）切换到iframe页面
         self.publish_page.find_iframe()：获取iframe对象
        """
        UtilsDriver.get_mp_driver().switch_to.frame(self.publish_page.find_iframe())
        # 输入内容
        self.input_text(self.publish_page.find_content(), content)
        # 切换到默认的首页以便后续操作
        UtilsDriver.get_mp_driver().switch_to.default_content()

    # 选择封面
    def choice_cover(self):
        self.publish_page.find_cover().click()

    # 选择频道
    def choice_chanel(self, chanel):
        # UtilsDriver.get_mp_driver()这个可以在初始化时候引入因为我上面有注释就不麻烦了
        choice_channel(UtilsDriver.get_mp_driver(), self.publish_page.find_chanel(), chanel)

    # 点击发布按钮
    def click_publish_btn(self):
        self.publish_page.find_publish_btn().click()


# 定义业务层
class PublishProxy:
    def __init__(self):
        self.publish_handle = PublishHandle()  # 实例化操作层

    def publish_article(self, title, content, chanel):  # 发布信息
        # 输入标题
        self.publish_handle.input_title(title)
        # 输入文章内容
        self.publish_handle.input_iframe(content)
        # 选择封面
        self.publish_handle.choice_cover()
        # 选择频道
        self.publish_handle.choice_chanel(chanel)
        # 点击发布
        self.publish_handle.click_publish_btn()

    pass
